import Vue from 'vue'
import {dateFormat} from '@/common/util.js'

Vue.filter('NumFormat', function(value) {
	if(!value) return '0.00';

	let intPart = Number(value)|0; //获取整数部分
	let intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,'); 
	let floatPart = ".00"; //预定义小数部分
	let value2Array = value.split(".");

	//=2表示数据有小数位
	if(value2Array.length == 2) {
		floatPart = value2Array[1].toString(); //拿到小数部分
		if(floatPart.length == 1) { //补0,实际上用不着
			return intPartFormat + "." + floatPart + '0';
		} else {
			return intPartFormat + "." + floatPart;
		}
	} else {
		return intPartFormat + floatPart;
	}
})

Vue.filter('money', function(val) {
    val = val.toString().replace(/\$|\,/g,'');
    if(isNaN(val)) {
      val = "0";  
    } 
    let sign = (val == (val = Math.abs(val)));
    val = Math.floor(val*100+0.50000000001);
    let cents = val%100;
    val = Math.floor(val/100).toString();
    if(cents<10) {
       cents = "0" + cents
    }
    for (var i = 0; i < Math.floor((val.length-(1+i))/3); i++) {
        val = val.substring(0,val.length-(4*i+3))+',' + val.substring(val.length-(4*i+3));
    }
    return (((sign)?'':'') + val + '.' + cents);
})

Vue.filter('formatDate',function(time){
	 let date = new Date(time);
	 return dateFormat.formatDate(date, "yyyy-MM-dd hh:mm:ss");			
})

Vue.filter('backNo',function(cardNo){
	return cardNo.substr(-4);		
})

Vue.filter('bankNoStyle',function(num){
	if(num){
		return num.replace(/^(\d{6})\d+(\d{4})$/,"$1 **** **** $2")
	}
})

Vue.filter('bankStyle',function(num){
	if(num){
		return num.replace(/^\d+(\d{4})$/,"**** ***** **** $1")
	}
})
Vue.filter('idNoStyle',function(str){//身份证
   return str.replace(/^(.{6})(?:\w+)(.{4})$/, "$1 **** **** $2")
})

Vue.filter('phoneNoStyle',function(num){//手机号隐藏
  if(num){
	return num.replace(/^(\d{3})\d+(\d{4})$/, "$1 **** $2")
  }  
})

Vue.filter('formatDate1',function(time){
	 let date = new Date(time.replace(/-/g, '/'));
	 return dateFormat.formatDate(date, "yyyy年MM月dd日");			
})

Vue.filter('formatDate2',function(time){//
	 let data = time.replace(/T/g, ' ').replace(/-/g, '/');
	     data = data.substr(0, 19);
	 let date = new Date(data);
	 return dateFormat.formatDate(date, "yyyy-MM-dd hh:mm:ss");			
})

Vue.filter('formatDate3',function(time){
	 let date = new Date(time.replace(/-/g, '/'));
	 return dateFormat.formatDate(date, "yyyy-MM-dd");			
})